package com.tywj.buscustomerapp.view.activity;

import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.tywj.buscustomerapp.Config;
import com.tywj.buscustomerapp.R;
import com.tywj.buscustomerapp.utils.PermissionsChecker;
import com.tywj.buscustomerapp.utils.ToastUtils;
import com.umeng.analytics.MobclickAgent;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;

public class WelcomeActivity extends BaseActivity {
    @BindView(R.id.welcome_img) ImageView imageView;
    @BindView(R.id.countdown) TextView countdown;
    private PermissionsChecker mPermissionsChecker;
    //权限集合
    static final String[] PERMISSIONS = {
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
    };
    private static final int PERMISSIONS_REQUEST_STORAGE = 1;
    private int  recLen = 5;
    private Timer timer;
    private TimerTask task;

    @Override
    protected int getContentId() {
        return R.layout.activity_welcome;
    }

    @Override
    protected void initWidget(Bundle savedInstanceState) {
        super.initWidget(savedInstanceState);

        SharedPreferences setting = getApplication().getSharedPreferences(Config.STRING_INFO, 0);
        boolean isOpened = setting.getBoolean("isOpened",false);
        if (isOpened){
            if (mPermissionsChecker == null){
                mPermissionsChecker = new PermissionsChecker(this);
            }        if (mPermissionsChecker == null){
                mPermissionsChecker = new PermissionsChecker(this);
            }
            if (mPermissionsChecker.lacksPermissions(PERMISSIONS)){
                ActivityCompat.requestPermissions(WelcomeActivity.this, PERMISSIONS,PERMISSIONS_REQUEST_STORAGE);
            }
            startTime();
        }else{
            setting.edit().putBoolean("isOpened",true).commit();
            Intent intent = new Intent(this,FirstComeActivity.class);
            startActivity(intent);
            finish();
        }

    }

    @Override
    protected void initClick() {
        super.initClick();
        countdown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    private void startTime() {
        timer = new Timer();
        task = new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {      // UI thread
                    @Override
                    public void run() {
                        try{
                            recLen--;
                            countdown.setText("跳过"+recLen);
                            if(recLen < 1){
                                timer.cancel();
                                countdown.setVisibility(View.GONE);
                                finish();
                            }
                        }catch (Exception e){
                            finish();
                        }
                    }
                });
            }
        };
        timer.schedule(task, 1000, 1000);
    }



    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case PERMISSIONS_REQUEST_STORAGE: {
                // 如果取消权限，则返回的值为0
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                } else {
                    ToastUtils.show("请开启定位权限");
                }
                return;
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("欢迎页");
        MobclickAgent.onResume(this); //统计时长

    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("欢迎页");
        MobclickAgent.onPause(this);
    }
}
